﻿import ast
import csv
import sys
import math


majorIndex = 5
abilityIndex = 1
NatureIndex = 1
provinceIndex = 1
cityIndex = 1
areaIndex = 1


class VectorProcessor:
    def __init__(self, csv_file):
        self.csv_file = csv_file

    def load_IDVectors_from_csv(self, csv_file):
        IDVectors = []
        try:
            with open(csv_file, mode='r', encoding='utf-8') as file:
                reader = csv.reader(file)
                for row in reader:
                    # 假设每行有足够多的数据，提取前18个值作为向量
                    if len(row) >= 18:
                        vector = [int(row[i]) for i in range(0, 1)]  # 将字符串转换为浮点数
                        IDVectors.append(vector)
        except FileNotFoundError:
            print(f"文件 {csv_file} 未找到。")
        except Exception as e:
            print(f"加载CSV文件时发生错误：{e}")
        return IDVectors

    def load_NatureVectors_from_csv(self, csv_file):

        NatureVectors = []
        try:
            with open(csv_file, mode='r', encoding='utf-8') as file:
                reader = csv.reader(file)
                for row in reader:
                    # 假设每行有足够多的数据，提取前18个值作为向量
                    if len(row) >= 18:
                        vector = [int(row[i]) for i in range(1, 3)]  # 将字符串转换为浮点数
                        NatureVectors.append(vector)
        except FileNotFoundError:
            print(f"文件 {csv_file} 未找到。")
        except Exception as e:
            print(f"加载CSV文件时发生错误：{e}")
        return NatureVectors

    def load_AreaSVectors_from_csv(self, csv_file):

        AreaVectors = []
        try:
            with open(csv_file, mode='r', encoding='utf-8') as file:
                reader = csv.reader(file)
                for row in reader:
                    # 假设每行有足够多的数据，提取前18个值作为向量
                    if len(row) >= 18:
                        vector = [(row[i]) for i in range(3, 4)]  # 将字符串转换为浮点数
                        AreaVectors.append(vector)
        except FileNotFoundError:
            print(f"文件 {csv_file} 未找到。")
        except Exception as e:
            print(f"加载CSV文件时发生错误：{e}")
        return AreaVectors

    def load_AreaCVectors_from_csv(self, csv_file):

        AreaVectors = []
        try:
            with open(csv_file, mode='r', encoding='utf-8') as file:
                reader = csv.reader(file)
                for row in reader:
                    # 假设每行有足够多的数据，提取前18个值作为向量
                    if len(row) >= 18:
                        vector = [(row[i]) for i in range(4, 5)]  # 将字符串转换为浮点数
                        AreaVectors.append(vector)
        except FileNotFoundError:
            print(f"文件 {csv_file} 未找到。")
        except Exception as e:
            print(f"加载CSV文件时发生错误：{e}")
        return AreaVectors

    def load_MajorVectors_from_csv(self, csv_file):

        MajorVectors = []
        try:
            with open(csv_file, mode='r', encoding='utf-8') as file:
                reader = csv.reader(file)
                for row in reader:
                    # 假设每行有足够多的数据，提取前18个值作为向量
                    if len(row) >= 18:
                        vector = [int(row[i]) for i in range(5, 12)]  # 将字符串转换为浮点数
                        MajorVectors.append(vector)
        except FileNotFoundError:
            print(f"文件 {csv_file} 未找到。")
        except Exception as e:
            print(f"加载CSV文件时发生错误：{e}")
        return MajorVectors

    def load_AbilityVectors_from_csv(self, csv_file):

        AbilityVectors = []
        try:
            with open(csv_file, mode='r', encoding='utf-8') as file:
                reader = csv.reader(file)
                for row in reader:
                    # 假设每行有足够多的数据，提取前18个值作为向量
                    if len(row) >= 18:
                        vector = [float(row[i]) for i in range(12, 18)]  # 将字符串转换为浮点数
                        AbilityVectors.append(vector)
        except FileNotFoundError:
            print(f"文件 {csv_file} 未找到。")
        except Exception as e:
            print(f"加载CSV文件时发生错误：{e}")
        return AbilityVectors

    def Major_vector(self, vector1, vector2):
        if len(vector1) != len(vector2):
            raise ValueError("两个向量的长度必须相同")
        result = 0
        for i in range(len(vector1)):
            result += vector1[i] * vector2[i]
        return result

    def Ability_vector(self, vector1, vector2):
        dot_product = 0
        for i in range(len(vector1)):
            dot_product += vector1[i] * vector2[i]
        magnitude1 = 0
        for i in range(len(vector1)):
            magnitude1 += vector1[i] ** 2
            magnitude1 = math.sqrt(magnitude1)
        magnitude2 = 0
        for i in range(len(vector2)):
            magnitude2 += vector2[i] ** 2
            magnitude2 = math.sqrt(magnitude2)
        if magnitude1 == 0 or magnitude2 == 0:
            return 0  # 如果任一向量的模为0，余弦相似度为0
        cosine_sim = dot_product / (magnitude1 * magnitude2)
        return cosine_sim

    def Nature_vector(self, vector1, vector2):
        a, b = vector1
        c, d = vector2
        if a == c:
            if a == 1:
                score1 = 0.5
            else:
                score1 = 1.0
        else:
            score1 = 0.0
        if b == d:
            if b == 1:
                score2 = 0.5
            else:
                score2 = 1.0
        else:
            score2 = 0.0
        result = (score1 + score2) / 2
        return result

    def AreaS_vector(self, a, c):
        if a == c:
            score1 = 2.0
        else:
            score1 = 0.0
        result = score1
        return result

    def AreaC_vector(self, b, d):
        if b == d:
            score2 = 1.0
        else:
            score2 = 0.0
        result = score2
        return result

    def normalize_vector(self, vector):
        min_val = min(vector)
        max_val = max(vector)
        if min_val == max_val:  # 避免除以零
            return [0.0] * len(vector)
        return [(x - min_val) / (max_val - min_val) for x in vector]


# 示例用法
if __name__ == "__main__":
    csv_file = "D:\desktop\server\src\main\java\com\egao\common\module\AI\model\Comp.csv"  # 替换为你的CSV文件路径
    processor = VectorProcessor(csv_file)
    max_sum = -float('inf')  # 初始化为负无穷大，确保任何实际值都会比它大
    max_index = -1  # 初始化为无效索引
    IDVectors = processor.load_IDVectors_from_csv(csv_file)
    MajorVectors = processor.load_MajorVectors_from_csv(csv_file)
    AbilityVectors = processor.load_AbilityVectors_from_csv(csv_file)
    NatureVectors = processor.load_NatureVectors_from_csv(csv_file)
    AreaSVectors = processor.load_AreaSVectors_from_csv(csv_file)
    AreaCVectors = processor.load_AreaCVectors_from_csv(csv_file)
    for i in range(1, 1710):
        MajorVector = ast.literal_eval(sys.argv[1])
        a = processor.Major_vector(MajorVector, MajorVectors[i]) / 7
        AbilityVector = ast.literal_eval(sys.argv[2])
        # AbilityVector = [0.6, 0.8, 0.5, 0.5, 0.1, 0.3]
        b = processor.Ability_vector(AbilityVector, AbilityVectors[i])
        NatureVector = ast.literal_eval(sys.argv[3])
        # NatureVector = [6, 11]
        c = processor.Nature_vector(NatureVector, NatureVectors[i])
        AreaSVector = ast.literal_eval(sys.argv[4])
        d = processor.AreaS_vector(AreaSVector, AreaSVectors[i])
        AreaCVector = ast.literal_eval(sys.argv[5])
        e = processor.AreaC_vector(AreaCVector, AreaCVectors[i])
        d1 = (d*provinceIndex + e*cityIndex) / 3
        # 湖北省 武汉市
        current_sum = a * majorIndex + b * abilityIndex + c * NatureIndex + d1 * areaIndex  # 计算当前行的总和

        if current_sum > max_sum:  # 如果当前总和大于最大值
            max_sum = current_sum  # 更新最大值
            max_index = IDVectors[i]  # 更新最大值对应的索引
    print(max_index)
